package com.zte.office.control;

import com.zte.office.model.Contacts;
import com.zte.office.model.ContactsType;
import com.zte.office.model.User;
import com.zte.office.model.contidion.ContactsCondition;
import com.zte.office.model.contidion.ContactsTypeCondition;
import com.zte.office.service.IOfficeService;
import com.zte.util.PageModel;
import org.apache.catalina.session.StandardSession;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author luode
 * @version 1.0
 * @date 2022/10/26 星期三 09:50
 */
@Controller
@RequestMapping("/office")
public class OfficeAction {
    @Autowired
    private IOfficeService service;

    public IOfficeService getService() {
        return service;
    }

    public void setService(IOfficeService service) {
        this.service = service;
    }

    /**
     * 分页查询名片夹分类列表
     * @param conn
     * @return
     */
    @RequestMapping("/queryContactsTypeList")
    public ModelAndView queryContactsTypeList(PageModel<ContactsType> pageModel, ContactsTypeCondition conn,HttpSession session){
        User user = new User();
        user.setUserId(1);
        user.setUname("熊子轩");
        session.setAttribute("user",user);
        PageModel<ContactsType> contactsTypePageModel = service.queryContactsTypeList(pageModel, conn);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("office/contacts/contactsTypeList");
        mv.addObject("ContactsTypeCondition",conn);
        mv.addObject("contactsTypePageModel",contactsTypePageModel);
        mv.addObject("statusList",service.queryTypeStatusList());
        return mv;
    }

    /**
     * 分页查询个人名片
     * @param pageModel
     * @param conn
     * @param session
     * @return
     */
    @RequestMapping("/queryContactsTypeListByUser")
    public ModelAndView queryContactsTypeListByUser(PageModel<ContactsType> pageModel, ContactsTypeCondition conn,HttpSession session){
        User user = (User) session.getAttribute("user");
        conn.setCreateStuffId(user.getUserId());
        PageModel<ContactsType> contactsTypePageModel = service.queryContactsTypeList(pageModel, conn);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("office/contacts/contactsTypeList");
        mv.addObject("contactsTypePageModel",contactsTypePageModel);
        return mv;
    }

    /**
     * 请求contactsAdd页面
     * @return
     */
    @RequestMapping("/contactsAdd")
    public String contactsAdd(HttpSession session){
        return "office/contacts/contactsAdd";
    }

    /**
     * 添加文件夹
     * @param contacts
     * @param session
     * @return
     */
    @RequestMapping("/AddContact")
    @ResponseBody
    public Boolean AddContact(ContactsType contacts,HttpSession session){
        User user = (User) session.getAttribute("user");
        contacts.setCreateStuffId(user.getUserId());
        return service.AddContact(contacts);
    }

    @RequestMapping("/updateContact")
    @ResponseBody
    public Boolean updateContact(ContactsType contacts,HttpSession session){
        User user = (User) session.getAttribute("user");
        contacts.setCreateStuffId(user.getUserId());
        int i = service.updateContact(contacts);
        return i>0;
    }

    /**
     * 查询名片夹中的名片
     * @param conn
     * @return
     */
    @RequestMapping("/queryContactsList")
    public ModelAndView queryContactsList(PageModel<Contacts> pageModel,ContactsCondition conn){
        ModelAndView mv = new ModelAndView();
        PageModel<Contacts> contactsPageModel = service.queryContactsList(pageModel,conn);
        mv.addObject("contactsPageModel",contactsPageModel);
        mv.setViewName("office/contacts/contactsView");
        return mv;
    }
    /**
     * 查询我的名片夹中的名片
     * @param conn
     * @return
     */
    @RequestMapping("/queryContactsListByUser")
    public ModelAndView queryContactsListByUser(HttpSession session,PageModel<Contacts> pageModel,ContactsCondition conn){
        ModelAndView mv = new ModelAndView();
        User user = (User) session.getAttribute("user");
        conn.setUserId(user.getUserId());
        PageModel<Contacts> contactsPageModel = service.queryContactsList(pageModel,conn);
        mv.addObject("contactsPageModel",contactsPageModel);
        mv.setViewName("office/contacts/personalContactsList");
        return mv;
    }

    /**
     * 删除名片夹
     * @param ids
     * @return
     */
    @RequestMapping("/removeContactType")
    @ResponseBody
    public boolean removeContactType(@RequestParam("ids[]") String[] ids) {
        System.out.println(ids);
        return service.removeContactType(ids);
    }

}
